package com.example.wasaimovie.Config;

import com.alibaba.fastjson.JSON;
import com.alipay.api.AlipayApiException;
import com.alipay.api.AlipayClient;
import com.alipay.api.DefaultAlipayClient;
import com.alipay.api.request.AlipayTradePagePayRequest;
import com.example.wasaimovie.Model.entity.AlipayBean;
import lombok.Data;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.context.annotation.Configuration;

@Data
@Configuration
public class Alipay {
    /**日志对象*/
    private static final Logger logger = LoggerFactory.getLogger(Alipay.class);

    private final String format = "json";

    /**
     * appId
     */
    @Value("2021000120613065")
    private String appId;

    /**
     * 商户私钥
     */
    @Value("MIIEvQIBADANBgkqhkiG9w0BAQEFAASCBKcwggSjAgEAAoIBAQCLQ/CIy1ZNPciU/NLmi65UqDwoZ71TDLwZfxU+iNHBpjKjSOO/MbZke86jKIK4nRFuJ6AdXNMS0zyROdKDIAULeEsGk05bMVDWogQLUKqgvB/GmLBwwwdnEiMD99pTjeyBg5DFxlMM69bjtEEYjSzGsxanQtOufQ1/XV7Ou1XfRfrCm9b8qgSTkn2THcCAmN4gS6DPYORlO+Q/sOFhr2i1xkzU3zsorISw57Urumwi1G6D8eID99SlWdW1XQy5T7jf244e8KU+6S9zo8ZeoZQC+O7WVi37PZSHZRol2748gb2gwuMIPYM8cxK1L5wMoXOqc3JN3AXBF9vdV+/WtbcfAgMBAAECggEACbG3Aq602TedyNE4lRfQENCNL3rZMPjH9zZjcjDv+PzqtXcTc3GwcpozriPZxaQZXIOhn6mgf8T4iFtwDfSipUPAZTrHz41FRUuN4IPC/OFbjOWAErlgwIoAqfwM1JgiiHlyEHJXwaSBFuSrS1w4a7R3tTjvp0vJLr5/JQDhX2NSJ++h2cPK+lfIRvBTbaP/nIOOutFrVwKAJoKB3PkBhL/BQ0tRwlMqriyu+bv1JgDh1eIdRNv0ui0jMSrqNC0Heelt2o6H9XlpzpPXKEcNRTb8qagEn/uxesE09Bw4vx7Gof6az5BEi82FLP5MrmkZL5OcuHQXsOvCqReioKkisQKBgQD/8+ALU2lOL8FlKnk9GHBEz4GdBPDj3G4qd2IDHLQkcvRbhRoghYrxMd1wsk9vcARuKG6BbbYJ/PvzNVh2q/0yJ9UMvnAIIxOUUPz+arYEDryjWZ8KQ/7pPuyIYtW8vtng969koWbepCrDqg2vcfxbFQSHhODQR/PclqRpnSVQHQKBgQCLSolqY+nqdSuE5WkCkSe+NgMu3dheVfjB6yzXVx0+xoIlN0VTVYb6HN+OEx9n4bk6Odi4hZP34SnYRRcjvTttE8erPXbu0B0Gipj90TqZQRmCjoWhMMYM5jrbwrupFuDjsBPJBrUxGs3PLtoAipuw9MkFt4ovBOZLGMgwzNM3awKBgQCe2dq47MG0dssOYHfyRRKYgjitHP3sTUhKLuGzkML0xCuICWmXuYnr+8i1QtUuElWNkKZUyaRrOomb/gWzUTbJRBNj32Vkh3CFYmZB+DSS/BpzPxIHqv+v/w+J7d09US2kA9TlHY7cqwKngQkRVCsM1FSFTyC/44gj5ZKQwakAuQKBgDe89uoU2zjP9D800eWOElpCINnzOi7dtjJbGlF9cZ16MAAWcu5a7i1NuoOO0WVvZQCQv1FvWnu5pVX+p7jMc2g9nwu+B8aejs7cTUZcIFfNPhkJUR3kd4wX4PupnBCYsBtMjU5HGeBxemPqup+214BjI8He5TPVgRYR76HfhLHLAoGAHPaGikFUqTGSPd4Ts+VrPatRVoxlqYVx2UK93Ywmfj10zXnB4690TIm/4RLplNKdJX6AhqQ5MyikwVd5R73lZPnNrAVs3gtYAxVz1CXoxhEJIIZbc5oAMTHJNoCrrC3P8Cun/8SpV8nfRboDPTI539ncf33op72vZzBmzTdt+5c=")
    private String privateKey;

    /**
     * 支付宝公钥
     */
    @Value("MIIBIjANBgkqhkiG9w0BAQEFAAOCAQ8AMIIBCgKCAQEAlrn6ZRfjSaWRC4wADFsZazK0h47ajYczfyJ7nrnzUpEBv5FHJiRS/tjPK7Nx6+cRha0x95+Lkh35AmKhHJuCSZCcPk3P21mB+y7TL3LW7WK8w6f+4kxR5f3BT3fAxGfq4y4NHAKnN8X5UlEsJs7mrZKPPQDimD/RDSZCGeeOAQXwnhyZAIk8UVe2F96QS5d7jaC7Zru7o6G0r4xVfifbaGeIatrMLaNs6n/P975Q6sLxVSOwqGFr3EkYJnfULZVf86a1UCcdMy+cHRm1NUntuy7EQ1ueE20TryWJjP8VO9wdRaat+oYwZx2WP54On/DTaTNYA40kEQO1tUqC2nFmOwIDAQAB")
    private String publicKey;

    /**
     * 服务器异步通知页面路径，需要公网能访问到
     */
    @Value("http://localhost:8080/ssm/error_url.html")
    private String notifyUrl;

    /**
     * 服务器同步通知页面路径，填写自己的成功页面路径
     */
    @Value("http://localhost:8080/success")
    private String returnUrl;

    /**
     * 签名方式
     */
    @Value("RSA2")
    private String signType;

    /**
     * 字符编码格式
     */
    @Value("utf-8")
    private String charset;

    /**
     * 支付宝网关
     */
    @Value("https://openapi.alipaydev.com/gateway.do")
    private String gatewayUrl;

    public String pay(AlipayBean aliPayBean) throws AlipayApiException {

        AlipayClient alipayClient = new DefaultAlipayClient(
                gatewayUrl, appId, privateKey, format, charset, publicKey, signType);

        System.out.println(alipayClient);
        AlipayTradePagePayRequest alipayRequest = new AlipayTradePagePayRequest();
        alipayRequest.setReturnUrl(returnUrl);
        alipayRequest.setNotifyUrl(notifyUrl);
        alipayRequest.setBizContent(JSON.toJSONString(aliPayBean));
        logger.info("封装请求支付宝付款参数为:{}", JSON.toJSONString(alipayRequest));

        String result = alipayClient.pageExecute(alipayRequest).getBody();
        logger.info("请求支付宝付款返回参数为:{}", result);

        return result;
    }
}
